/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.report.service;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.base.entity.StoreCare;
import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.cash.dao.StoreConsumeBuyProductDao;
import com.chlitina.store.modules.cash.dao.StoreConsumeDao;
import com.chlitina.store.modules.cash.dao.StoreConsumeDoCareDao;
import com.chlitina.store.modules.cash.dao.StoreConsumeNewCardDao;
import com.chlitina.store.modules.cash.dao.StoreConsumeRechargeCardDao;
import com.chlitina.store.modules.cash.entity.StoreConsume;
import com.chlitina.store.modules.cash.entity.StoreConsumeBuyProduct;
import com.chlitina.store.modules.cash.entity.StoreConsumeDoCare;
import com.chlitina.store.modules.cash.entity.StoreConsumeNewCard;
import com.chlitina.store.modules.cash.entity.StoreConsumeRechargeCard;
import com.chlitina.store.modules.cash.model.Card;
import com.chlitina.store.modules.cash.service.StoreConsumeBuyProductService;
import com.chlitina.store.modules.cash.service.StoreConsumeDoCareService;
import com.chlitina.store.modules.cash.service.StoreConsumeNewCardService;
import com.chlitina.store.modules.cash.service.StoreConsumeRechargeCardService;
import com.chlitina.store.modules.cash.utils.StoreCashUtils;
import com.chlitina.store.modules.employee.entity.Employee;
import com.chlitina.store.modules.employee.model.EmployeeAccounting;
import com.chlitina.store.modules.employee.service.EmployeeService;
import com.chlitina.store.modules.expert.entity.CustomerSolution;
import com.chlitina.store.modules.expert.entity.CustomerSolutionAndCare;
import com.chlitina.store.modules.expert.entity.CustomerSolutionAndProduct;
import com.chlitina.store.modules.expert.service.CustomerSolutionAndCareService;
import com.chlitina.store.modules.expert.service.CustomerSolutionAndProductService;
import com.chlitina.store.modules.expert.service.CustomerSolutionService;
import com.chlitina.store.modules.report.model.BusinessAll;
import com.chlitina.store.modules.report.model.ReportCard;
import com.chlitina.store.modules.report.model.ReportCare;
import com.chlitina.store.modules.report.model.ReportEmployeeBonusDetail;
import com.chlitina.store.modules.report.model.ReportProduct;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * 报表Service
 * @author lvbo
 * @version 2013-06-21
 */
@Component
@Transactional(readOnly = true)
public class ReportService extends BaseService {
	@Autowired
	private StoreConsumeDao storeConsumeDao;
	@Autowired
	private StoreConsumeBuyProductDao storeConsumeBuyProductDao;
	@Autowired
	private StoreConsumeDoCareDao storeConsumeDoCareDao;
	@Autowired
	private StoreConsumeNewCardDao storeConsumeNewCardDao;
	@Autowired
	private StoreConsumeRechargeCardDao storeConsumeRechargeCardDao;
	@Autowired
	private CustomerSolutionService customerSolutionService;
	@Autowired
	private CustomerSolutionAndCareService customerSolutionAndCareService;
	@Autowired
	private CustomerSolutionAndProductService customerSolutionAndProductService;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private StoreConsumeNewCardService storeConsumeNewCardService;
	@Autowired
	private StoreConsumeRechargeCardService storeConsumeRechargeCardService;
	@Autowired
	private StoreConsumeDoCareService storeConsumeDoCareService;
	@Autowired
	private StoreConsumeBuyProductService storeConsumeBuyProductService;
	
	/*
	 * 获取指定时间段内的全部方案
	 */
	public List<CustomerSolution> findSolutionByDuration(String startDate, String endDate){
		Store store = StoreUtils.getStore();
		Date start = null;
		Date end = null;
		try {
			start = DateUtils.parseDate(startDate);
			start = DateUtils.getBeginOfDay(start);
			end = DateUtils.parseDate(endDate);
			end = DateUtils.getEndOfDay(end);
		} catch (Exception e) {
		}
		return customerSolutionService.findAllCustomerSolution(store, start, end);
	}
	/*
	 * 获取指定时间段内的全部消费
	 */
	public List<StoreConsume> findConsumeByDuration(String startDate, String endDate){
		DetachedCriteria dc = storeConsumeDao.createDetachedCriteria();
		Store store = StoreUtils.getStore();
		dc.add(Restrictions.eq("store.id", store.getId()));
		Date start = null;
		Date end = null;
		try {
			start = DateUtils.parseDate(startDate);
			start = DateUtils.getBeginOfDay(start);
			end = DateUtils.parseDate(endDate);
			end = DateUtils.getEndOfDay(end);
		} catch (Exception e) {
		}
		if (start != null && end == null) {
			dc.add(Restrictions.ge("consume_Time", start));
		} else if (start == null && end != null) {
			dc.add(Restrictions.le("consume_Time", end));
		} else if (start != null && end != null) {
			dc.add(Restrictions.between("consume_Time", start, end));
		}
		//状态已确认
		dc.add(Restrictions.eq("confirmed", StoreConsume.TYPE_CONFIRMED));
		return storeConsumeDao.find(dc);
	}
	/*
	 * 获取指定时间段内的全部收入
	 */
	public BusinessAll findAll(String startDate, String endDate){
		List<StoreConsume> list = findConsumeByDuration(startDate, endDate);
		float pMoney = 0;
		float cMoney = 0;
		float nMoney = 0;
		float rMoney = 0;
		float bMoney = 0;
		int solutionSize = 0;
		if (list != null && list.size() > 0) {
			for (StoreConsume sc : list) {
				Long consumeId = sc.getId();
				//产品
				List<StoreConsumeBuyProduct> plist = storeConsumeBuyProductDao.findListByConsume(consumeId);
				for (StoreConsumeBuyProduct scbp : plist) {
					pMoney += scbp.getReal_cost_total();
					bMoney += scbp.getBonus();
				}
				//护理
				List<StoreConsumeDoCare> clist = storeConsumeDoCareDao.findListByConsume(consumeId);
				for (StoreConsumeDoCare scdc : clist) {
					cMoney += scdc.getReal_cost();
					bMoney += scdc.getBonus();
				}
				//新开卡
				List<StoreConsumeNewCard> nlist = storeConsumeNewCardDao.findListByConsume(consumeId);
				for (StoreConsumeNewCard scnc : nlist) {
					nMoney += scnc.getReal_cost();
					bMoney += scnc.getBonus();
				}
				//卡充值
				List<StoreConsumeRechargeCard> rlist = storeConsumeRechargeCardDao.findListByConsume(consumeId);
				for (StoreConsumeRechargeCard scrc : rlist) {
					rMoney += scrc.getReal_cost();
					bMoney += scrc.getBonus();
				}
			}
		}
		//方案个数
		List<CustomerSolution> sList = findSolutionByDuration(startDate, endDate);
		if (sList !=null && sList.size() > 0) {
			solutionSize = sList.size();
		}
		BusinessAll all = new BusinessAll();
		all.setBuyProduct(pMoney);
		all.setDoCare(cMoney);
		all.setNewCard(nMoney);
		all.setRechargeCard(rMoney);
		all.setEmployeeBonus(bMoney);
		all.setSolution(solutionSize);
		return all;
	}
	public List<ReportProduct> getSolutionByDuration(String startDate, String endDate){
		
		return null;
	}
	public List<ReportProduct> findProductByDuration(String startDate, String endDate){
		//报表产品
		List<ReportProduct> reportProductList = Lists.newArrayList();
		//产品列表 不能重复
		Set<StoreProduct> spSet = Sets.newHashSet();
		//所有StoreConsumeBuyProduct对象列表
		List<StoreConsumeBuyProduct> allList = Lists.newArrayList();
		List<StoreConsume> consumeList = findConsumeByDuration(startDate, endDate);
		for (StoreConsume sc : consumeList) {
			List<StoreConsumeBuyProduct> bpList = storeConsumeBuyProductDao.findListByConsume(sc.getId());
			allList.addAll(bpList);
		}
		//添加产品
		for (StoreConsumeBuyProduct scbp : allList) {
			spSet.add(scbp.getStoreProduct());
		}
		//添加报表产品
		Iterator<StoreProduct> iterator = spSet.iterator();
		while (iterator.hasNext()) {
			ReportProduct rp = new ReportProduct();
			int saleNumber = 0;//销售数量
			float totalPrice = 0;//总价
			StoreProduct sp = iterator.next();
			for (StoreConsumeBuyProduct scbp : allList) {
				if (scbp.getStoreProduct().getId().equals(sp.getId())) {
					saleNumber += scbp.getAmount();
					totalPrice += scbp.getReal_cost_total();
				}
			}
			rp.setProduct(sp);
			rp.setSaleNumber(saleNumber);
			rp.setTotalPrice(totalPrice);
			reportProductList.add(rp);
		}
		return reportProductList;
	}
	public List<EmployeeAccounting> findBonusByDuration(String startDate, String endDate){
		List<EmployeeAccounting> list = Lists.newArrayList();
		try {
			Date start = null;
			Date end = null;
			if (!StringUtils.isEmpty(startDate)) {
				start = DateUtils.getBeginOfDay(DateUtils.parseDate(startDate));
			}
			if (!StringUtils.isEmpty(endDate)) {
				end = DateUtils.getEndOfDay(DateUtils.parseDate(endDate));
			}
			List<Employee> employeeList = employeeService.findByStore(StoreUtils.getStore(), Employee.STATUS_WORKING);
			for (Employee employee : employeeList) {
				float newCardBonus = storeConsumeNewCardService.getByEmployeeBonus(employee, start, end);
				float rechargeCardBonus = storeConsumeRechargeCardService.getByEmployeeBonus(employee, start, end);
				float doCareBonus = storeConsumeDoCareService.getByEmployeeBonus(employee, start, end);
				float productSaleBonus = storeConsumeBuyProductService.getByEmployeeBonus(employee, start, end);
				EmployeeAccounting detail = new EmployeeAccounting();
				detail.setEmployeeId(employee.getId());
				detail.setEmployeeName(employee.getName());
				detail.setNew_card_bonus(newCardBonus);
				detail.setRecharge_card_bonus(rechargeCardBonus);
				detail.setDo_care_bonus(doCareBonus);
				detail.setProduct_sale_bonus(productSaleBonus);
				float total = newCardBonus + rechargeCardBonus + doCareBonus + productSaleBonus;
				float sum = (float)(Math.round(total*100))/100;
				detail.setTotal(sum);
				if(sum != 0) {
					list.add(detail);
				}
			}
		} catch (Exception e) {
		}
		return list;
	}
	@SuppressWarnings("unchecked")
	public List<ReportCard> findCardByDuration(String startDate, String endDate){
		Store store = StoreUtils.getStore();
		List<ReportCard> newCardList = Lists.newArrayList();
		Timestamp st = null;
		Timestamp et = null;
		try {
			Date start = DateUtils.getBeginOfDay(DateUtils.parseDate(startDate));
			st = new Timestamp(start.getTime());
		} catch (Exception e) {
			st = null;
		}
		try {
			Date end = DateUtils.getEndOfDay(DateUtils.parseDate(endDate));
			et = new Timestamp(end.getTime());
		} catch (Exception e) {
			et = null;
		}
		try {
			EntityManager em = storeConsumeBuyProductDao.getEntityManager();
			
			//新开卡
			String timesCardSqlNew = "SELECT sctt.id,sctt.name,count(DISTINCT scnc.id),scnc.card_type ";
			String durationCardSqlNew = "SELECT sctd.id,sctd.name,count(DISTINCT scnc.id),scnc.card_type ";
			String productCardSqlNew = "SELECT sctp.id,sctp.name,count(DISTINCT scnc.id),scnc.card_type ";
			String allCardSqlNew = "SELECT scta.id,scta.name,count(DISTINCT scnc.id),scnc.card_type ";
			String tableNew =" FROM" +
						"	StoreConsume sc, " +
						"	StoreConsumeNewCard scnc," +
						"	StoreCardTimes sct, " +
						"	StoreCardProduct scp," +
						"	StoreCardAll sca, " +
						"	StoreCardDuration scd," +
						"	StoreCardTemplateAll scta," +
						"	StoreCardTemplateDuration sctd," +
						"	StoreCardTemplateProduct sctp," +
						"	StoreCardTemplateTimes sctt";
			
			String utilQueryNew = " WHERE sc.id=scnc.storeConsume.id AND sc.store.id=" + store.getId();
			String timesCardQueryNew = " AND scnc.card_id=sct.id AND sct.template.id=sctt.id AND scnc.card_type=0";
			String durationCardQueryNew = " AND scnc.card_id=scd.id AND scd.template.id=sctd.id AND scnc.card_type=1";
			String productCardQueryNew = " AND scnc.card_id=scp.id AND scp.template.id=sctp.id AND scnc.card_type=2";
			String allCardQueryNew = " AND scnc.card_id=sca.id AND sca.template.id=scta.id AND scnc.card_type=3";
			StringBuffer timessbNew = new StringBuffer();
			timessbNew.append(timesCardSqlNew);
			timessbNew.append(tableNew);
			timessbNew.append(utilQueryNew);
			timessbNew.append(timesCardQueryNew);
			
			StringBuffer durationsbNew = new StringBuffer();
			durationsbNew.append(durationCardSqlNew);
			durationsbNew.append(tableNew);
			durationsbNew.append(utilQueryNew);
			durationsbNew.append(durationCardQueryNew);
			
			StringBuffer productsbNew = new StringBuffer();
			productsbNew.append(productCardSqlNew);
			productsbNew.append(tableNew);
			productsbNew.append(utilQueryNew);
			productsbNew.append(productCardQueryNew);
			
			StringBuffer allCardsbNew = new StringBuffer();
			allCardsbNew.append(allCardSqlNew);
			allCardsbNew.append(tableNew);
			allCardsbNew.append(utilQueryNew);
			allCardsbNew.append(allCardQueryNew);
			
			if (st != null) {
				timessbNew.append(" AND sc.consume_Time > '" + st + "'");
				durationsbNew.append(" AND sc.consume_Time > '" + st + "'");
				productsbNew.append(" AND sc.consume_Time > '" + st + "'");
				allCardsbNew.append(" AND sc.consume_Time > '" + st + "'");
			}
			if (et != null) {
				timessbNew.append(" AND sc.consume_Time < '" + et + "'");
				durationsbNew.append(" AND sc.consume_Time < '" + et + "'");
				productsbNew.append(" AND sc.consume_Time < '" + et + "'");
				allCardsbNew.append(" AND sc.consume_Time < '" + et + "'");
			}
			List listNew = Lists.newLinkedList();
			Query queryNew = em.createQuery(allCardsbNew.toString());
			listNew.addAll(queryNew.getResultList());
			queryNew = em.createQuery(timessbNew.toString());
			listNew.addAll(queryNew.getResultList());
			queryNew = em.createQuery(durationsbNew.toString());
			listNew.addAll(queryNew.getResultList());
			queryNew = em.createQuery(productsbNew.toString());
			listNew.addAll(queryNew.getResultList());
			//卡充值
			String timesCardSqlRecharge = "SELECT sctt.id,sctt.name,count(DISTINCT scnc.id),scnc.card_type ";
			String durationCardSqlRecharge = "SELECT sctd.id,sctd.name,count(DISTINCT scnc.id),scnc.card_type ";
			String productCardSqlRecharge = "SELECT sctp.id,sctp.name,count(DISTINCT scnc.id),scnc.card_type ";
			String allCardSqlRecharge = "SELECT scta.id,scta.name,count(DISTINCT scnc.id),scnc.card_type ";
			String tableRecharge =" FROM" +
						"	StoreConsume sc, " +
						"	StoreConsumeRechargeCard scnc," +
						"	StoreCardTimes sct, " +
						"	StoreCardProduct scp," +
						"	StoreCardAll sca, " +
						"	StoreCardDuration scd," +
						"	StoreCardTemplateAll scta," +
						"	StoreCardTemplateDuration sctd," +
						"	StoreCardTemplateProduct sctp," +
						"	StoreCardTemplateTimes sctt";
			
			String utilQueryRecharge = " WHERE sc.id=scnc.storeConsume.id AND sc.store.id=" + store.getId();
			String timesCardQueryRecharge = " AND scnc.card_id=sct.id AND sct.template.id=sctt.id AND scnc.card_type=0";
			String durationCardQueryRecharge = " AND scnc.card_id=scd.id AND scd.template.id=sctd.id AND scnc.card_type=1";
			String productCardQueryRecharge = " AND scnc.card_id=scp.id AND scp.template.id=sctp.id AND scnc.card_type=2";
			String allCardQueryRecharge = " AND scnc.card_id=sca.id AND sca.template.id=scta.id AND scnc.card_type=3";
			StringBuffer timessbRecharge = new StringBuffer();
			timessbRecharge.append(timesCardSqlRecharge);
			timessbRecharge.append(tableRecharge);
			timessbRecharge.append(utilQueryRecharge);
			timessbRecharge.append(timesCardQueryRecharge);
			
			StringBuffer durationsbRecharge = new StringBuffer();
			durationsbRecharge.append(durationCardSqlRecharge);
			durationsbRecharge.append(tableRecharge);
			durationsbRecharge.append(utilQueryRecharge);
			durationsbRecharge.append(durationCardQueryRecharge);
			
			StringBuffer productsbRecharge = new StringBuffer();
			productsbRecharge.append(productCardSqlRecharge);
			productsbRecharge.append(tableRecharge);
			productsbRecharge.append(utilQueryRecharge);
			productsbRecharge.append(productCardQueryRecharge);
			
			StringBuffer allCardsbRecharge = new StringBuffer();
			allCardsbRecharge.append(allCardSqlRecharge);
			allCardsbRecharge.append(tableRecharge);
			allCardsbRecharge.append(utilQueryRecharge);
			allCardsbRecharge.append(allCardQueryRecharge);
			
			if (st != null) {
				timessbRecharge.append(" AND sc.consume_Time > '" + st + "'");
				durationsbRecharge.append(" AND sc.consume_Time > '" + st + "'");
				productsbRecharge.append(" AND sc.consume_Time > '" + st + "'");
				allCardsbRecharge.append(" AND sc.consume_Time > '" + st + "'");
			}
			if (et != null) {
				timessbRecharge.append(" AND sc.consume_Time < '" + et + "'");
				durationsbRecharge.append(" AND sc.consume_Time < '" + et + "'");
				productsbRecharge.append(" AND sc.consume_Time < '" + et + "'");
				allCardsbRecharge.append(" AND sc.consume_Time < '" + et + "'");
			}
			List listRecharge = Lists.newLinkedList();
			Query queryRecharge = em.createQuery(allCardsbRecharge.toString());
			listRecharge.addAll(queryRecharge.getResultList());
			queryRecharge = em.createQuery(timessbRecharge.toString());
			listRecharge.addAll(queryRecharge.getResultList());
			queryRecharge = em.createQuery(durationsbRecharge.toString());
			listRecharge.addAll(queryRecharge.getResultList());
			queryRecharge = em.createQuery(productsbRecharge.toString());
			listRecharge.addAll(queryRecharge.getResultList());
			
			Iterator<?> iteratorNew = listNew.iterator();
			while(iteratorNew.hasNext()){
				Object[] tuple = (Object[])iteratorNew.next();  
				if (tuple[0] != null) {
					ReportCard rc = new ReportCard();
					rc.setTempleId(Integer.parseInt(tuple[0].toString()));
					rc.setCardName(tuple[1].toString());
					rc.setSaleNumber(Integer.parseInt(tuple[2].toString()));
					rc.setCardType(Integer.parseInt(tuple[3].toString()));
					rc.setReportType(ReportCard.REPORT_TYPE_NEW);
					newCardList.add(rc);
				}
			}
			Iterator<?> iteratorRecharge = listRecharge.iterator();
			while(iteratorRecharge.hasNext()){
				Object[] tuple = (Object[])iteratorRecharge.next();  
				if (tuple[0] != null) {
					ReportCard rc = new ReportCard();
					rc.setTempleId(Integer.parseInt(tuple[0].toString()));
					rc.setCardName(tuple[1].toString());
					rc.setSaleNumber(Integer.parseInt(tuple[2].toString()));
					rc.setCardType(Integer.parseInt(tuple[3].toString()));
					rc.setReportType(ReportCard.REPORT_TYPE_RECHARGE);
					newCardList.add(rc);
				}
			}
		} catch (Exception ex) {
			System.out.println(ex);
		}
		return newCardList;
	}
	public List<ReportCare> findCareByDuration(String startDate, String endDate){
		//报表护理
		List<ReportCare> reportCareList = Lists.newArrayList();
		//护理列表 不能重复
		Set<StoreCare> scSet = Sets.newHashSet();
		//所有StoreConsumeDoCare对象列表
		List<StoreConsumeDoCare> allList = Lists.newArrayList();
		List<StoreConsume> consumeList = findConsumeByDuration(startDate, endDate);
		for (StoreConsume sc : consumeList) {
			List<StoreConsumeDoCare> dcList = storeConsumeDoCareDao.findListByConsume(Integer.parseInt(sc.getId() + ""));
			allList.addAll(dcList);
		}
		//添加护理
		for (StoreConsumeDoCare scdc : allList) {
			scSet.add(scdc.getStoreCare());
		}
		//添加报表护理
		Iterator<StoreCare> iterator = scSet.iterator();
		while (iterator.hasNext()) {
			ReportCare rc = new ReportCare();
			int saleNumber = 0;//销售数量
			float totalPrice = 0;//总价
			StoreCare sc = iterator.next();
			for (StoreConsumeDoCare scdc : allList) {
				if (scdc.getStoreCare().getId().equals(sc.getId())) {
					saleNumber ++;
					totalPrice += scdc.getReal_cost();
				}
			}
			rc.setCare(sc);
			rc.setSaleNumber(saleNumber);
			rc.setTotalPrice(totalPrice);
			reportCareList.add(rc);
		}
		return reportCareList;
	}
	public List<StoreConsumeBuyProduct> findProductDetailByDuration(String productId, String startDate, String endDate){
		List<StoreConsumeBuyProduct> productDetailList = Lists.newArrayList();
		List<StoreConsume> consumeList = findConsumeByDuration(startDate, endDate);
		List<StoreConsumeBuyProduct> scbpList = storeConsumeBuyProductDao.findListByProduct(Long.parseLong(productId));
		for (StoreConsumeBuyProduct scbp : scbpList) {
			for (StoreConsume sc : consumeList) {
				if (sc.getId().equals(scbp.getStoreConsume().getId())) {
					productDetailList.add(scbp);
				}
			}
		}
		return productDetailList;
	}
	public List<StoreConsumeDoCare> findCareDetailByDuration(String careId, String startDate, String endDate){
		List<StoreConsumeDoCare> careDetailList = Lists.newArrayList();
		List<StoreConsume> consumeList = findConsumeByDuration(startDate, endDate);
		List<StoreConsumeDoCare> scdcList = storeConsumeDoCareDao.findListByCare(Long.parseLong(careId));
		for (StoreConsumeDoCare scdc : scdcList) {
			for (StoreConsume sc : consumeList) {
				if (sc.getId().equals(scdc.getStoreConsume().getId())) {
					careDetailList.add(scdc);
				}
			}
		}
		return careDetailList;
	}
	public List<StoreConsumeNewCard> findNewCardDetailByDuration(String cardTmpId,String cardType, String startDate, String endDate){
		List<StoreConsumeNewCard> cardDetailList = Lists.newArrayList();
		List<StoreConsume> consumeList = findConsumeByDuration(startDate, endDate);
		List<StoreConsumeNewCard> scncList = storeConsumeNewCardDao.findListByCardType(Integer.parseInt(cardType));
		for (StoreConsumeNewCard scnc : scncList) {
			for (StoreConsume sc : consumeList) {
				if (sc.getId().equals(scnc.getStoreConsume().getId())) {
					Card card = StoreCashUtils.getCard(StoreCashUtils.CONSUME_NEW_CARD_OR_RECHANGE, Integer.parseInt(cardType), scnc.getCard_id());
					if (cardTmpId.equals(card.getTempleId() + "")) {
						cardDetailList.add(scnc);
					}
				}
			}
		}
		return cardDetailList;
	}
	public List<StoreConsumeRechargeCard> findRechargeCardDetailByDuration(String cardTmpId,String cardType, String startDate, String endDate){
		List<StoreConsumeRechargeCard> cardDetailList = Lists.newArrayList();
		List<StoreConsume> consumeList = findConsumeByDuration(startDate, endDate);
		List<StoreConsumeRechargeCard> scrcList = storeConsumeRechargeCardDao.findListByCardType(Integer.parseInt(cardType));
		for (StoreConsumeRechargeCard scrc : scrcList) {
			for (StoreConsume sc : consumeList) {
				if (sc.getId().equals(scrc.getStoreConsume().getId())) {
					Card card = StoreCashUtils.getCard(StoreCashUtils.CONSUME_NEW_CARD_OR_RECHANGE, Integer.parseInt(cardType), (long)scrc.getCard_id());
					if (cardTmpId.equals(card.getTempleId() + "")) {
						cardDetailList.add(scrc);
					}
				}
			}
		}
		return cardDetailList;
	}
	public ReportEmployeeBonusDetail findBonusDetailByDuration(String employeeId, String startDate, String endDate){
		Date start = null;
		Date end = null;
		if (!StringUtils.isEmpty(startDate)) {
			start = DateUtils.getBeginOfDay(DateUtils.parseDate(startDate));
		}
		if (!StringUtils.isEmpty(endDate)) {
			end = DateUtils.getEndOfDay(DateUtils.parseDate(endDate));
		}
		Employee employee = employeeService.get(Long.parseLong(employeeId));
		List<StoreConsumeNewCard> storeConsumeNewCardList = storeConsumeNewCardService.getByEmployee(employee, start, end);
		List<StoreConsumeRechargeCard> storeConsumeRechargeCardList = storeConsumeRechargeCardService.getByEmployee(employee, start, end);
		List<StoreConsumeBuyProduct> storeConsumeBuyProductList = storeConsumeBuyProductService.getByEmployee(employee, start, end);
		List<StoreConsumeDoCare> storeConsumeDoCareList = storeConsumeDoCareService.getByEmployee(employee, start, end);
		ReportEmployeeBonusDetail rebd = new ReportEmployeeBonusDetail();
		rebd.setStoreConsumeBuyProductList(storeConsumeBuyProductList);
		rebd.setStoreConsumeDoCareList(storeConsumeDoCareList);
		rebd.setStoreConsumeNewCardList(storeConsumeNewCardList);
		rebd.setStoreConsumeRechargeCardList(storeConsumeRechargeCardList);
		return rebd;
	}
	public CustomerSolution findSolutionDetailByDuration(String solutionId){
		CustomerSolution customerSolution = customerSolutionService.get(Long.parseLong(solutionId));
		List<CustomerSolutionAndCare> careList = customerSolutionAndCareService.find(Long.parseLong(solutionId));
		customerSolution.setCareList(careList);
		List<CustomerSolutionAndProduct> productList = customerSolutionAndProductService.find(Long.parseLong(solutionId));
		customerSolution.setProductList(productList);
		return customerSolution;
	}

}
